//
//  SWSegmentedControl.m
//  greenbuilding
//
//  Created by lvjian01 on 2017/9/12.
//  Copyright © 2017年 greenbuilding. All rights reserved.
//

#import "SWSegmentedControl.h"

@interface SWSegmentedControl () {
    CGFloat widthFloat;
    CALayer *bottomLayer;
    CALayer *bottomLongLayer;
}


/**
 按钮集合
 */
@property (nonatomic, strong) NSMutableArray *btnTitleSource;

@end


@implementation SWSegmentedControl

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self defaultSetting];
    }
    return self;
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        [self defaultSetting];
    }
    return self;
}

- (void)defaultSetting{
    self.btnTitleSource = [NSMutableArray array];
    self.titleSource = [NSMutableArray array];
    _selectSegument = 0;
    
    self.backgroundColor = [UIColor clearColor];
    self.bottomColor = UIColorFromRGB(0x88C14F);
    self.bottomLineColor = UIColorFromRGB(0xD4D4D4);
    self.titleColor = UIColorFromRGB(0xB1B3AF);
    self.titleFont = [UIFont systemFontOfSize:16];
    self.selectColor = UIColorFromRGB(0x88C14F);
    self.isAnimated = YES;
}

+(SWSegmentedControl *)segmentedControlFram:(CGRect)frame titleDataSource:(NSArray *)titleDataSource delegate:(id<SWSegmentedControlDelegate>)delegate{
    SWSegmentedControl *smc = [[self alloc] initWithFrame:frame];
    smc.delegate = delegate;
    smc.titleSource = titleDataSource;
    [smc addSegumentArray:titleDataSource];
    return smc;
}

+(SWSegmentedControl *)segmentedControlFrame:(CGRect)frame titleDataSource:(NSArray *)titleDataSource backgroundColor:(UIColor *)backgroundColor titleColor:(UIColor *)titleColor titleFont:(UIFont *)titleFont selectColor:(UIColor *)selectColor bottomColor:(UIColor *)bottomColor delegate:(id<SWSegmentedControlDelegate>)delegate{
    SWSegmentedControl *smc = [[self alloc] initWithFrame:frame];
    smc.backgroundColor = backgroundColor;
    smc.bottomColor = bottomColor;
    smc.titleColor = titleColor;
    smc.titleFont = titleFont;
    smc.selectColor = selectColor;
    smc.delegate = delegate;
    smc.titleSource = titleDataSource;
    [smc addSegumentArray:titleDataSource];
    return smc;
}

-(void)segmentedControl:(NSArray *)titleDataSource delegate:(id<SWSegmentedControlDelegate>)delegate{
    self.delegate = delegate;
    self.titleSource = titleDataSource;
    [self addSegumentArray:titleDataSource];
}

-(void)segmentedControl:(NSArray *)titleDataSource backgroundColor:(UIColor *)backgroundColor titleColor:(UIColor *)titleColor titleFont:(UIFont *)titleFont selectColor:(UIColor *)selectColor bottomColor:(UIColor *)bottomColor delegate:(id<SWSegmentedControlDelegate>)delegate{
    self.backgroundColor = backgroundColor;
    self.bottomColor = bottomColor;
    self.titleColor = titleColor;
    self.titleFont = titleFont;
    self.selectColor = selectColor;
    self.delegate = delegate;
    self.titleSource = titleDataSource;
    [self addSegumentArray:titleDataSource];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    //设置按钮的frame
    [self setSegumentFrame];
}

- (void)addSegumentArray:(NSArray *)segumentArray{
    
//#error 这里创建底部公用线
    bottomLongLayer = [[CALayer alloc] init];
    [self.layer addSublayer:bottomLongLayer];
    
    //创建按钮
    for (int i =0; i < segumentArray.count; i++) {
        UIButton *btn = [[UIButton alloc] init];
        btn.tag = i + 1;
        [btn addTarget:self action:@selector(changeSegumentAction:) forControlEvents:UIControlEventTouchUpInside];
        if (i == 0) {
            bottomLayer = [[CALayer alloc] init];
            bottomLayer.cornerRadius = 1;
            [self.layer addSublayer:bottomLayer];
        }
        [self addSubview:btn];
        [self.btnTitleSource addObject:btn];
    }

    
    [[self.btnTitleSource firstObject] setSelected:YES];
}

- (void)setSegumentFrame{
    
    bottomLongLayer.frame = CGRectMake(0, self.bounds.size.height-2, self.bounds.size.width, 2);
    bottomLongLayer.backgroundColor = self.bottomLineColor.CGColor;

    //按钮的个数
    NSInteger segumentNumber = self.titleSource.count;
    //按钮的宽度
    widthFloat = (self.bounds.size.width) / segumentNumber;
    //创建按钮
    for (int i = 0; i < segumentNumber; i++) {
        UIButton *btn = self.btnTitleSource[i];
        btn.frame = CGRectMake(i * widthFloat, 0, widthFloat, self.bounds.size.height - 4);
//        btn.frame = CGRectMake(i * widthFloat, 17, widthFloat, self.bounds.size.height - 21);
        [btn setTitle:self.titleSource[i] forState:UIControlStateNormal];
        [btn.titleLabel setFont:self.titleFont];
        [btn setTitleColor:self.titleColor forState:UIControlStateNormal];
        [btn setTitleColor:self.selectColor forState:UIControlStateSelected];
        if (i == _selectSegument) {
//            bottomLayer.frame = CGRectMake(i *widthFloat, self.bounds.size.height-8, widthFloat, 4);
            bottomLayer.frame = CGRectMake(i *widthFloat+12, self.bounds.size.height-2, widthFloat-24, 2);
            [bottomLayer setBackgroundColor:self.bottomColor.CGColor];
        }
    }
}

- (void)changeSegumentAction:(UIButton *)btn{
    [self selectTheSegument:btn.tag - 1];
}

-(void)selectTheSegument:(NSInteger)segument{
    if (_selectSegument != segument) {
        [self.btnTitleSource[_selectSegument] setSelected:NO];
        [self.btnTitleSource[segument] setSelected:YES];
        
        if (self.isAnimated) {
            [bottomLayer setFrame:CGRectMake(segument * widthFloat+12, self.bounds.size.height - 2, widthFloat-24, 2)];
        }else{
            [CATransaction begin];
            [CATransaction setDisableActions:YES];
            [bottomLayer setFrame:CGRectMake(segument * widthFloat+12, self.bounds.size.height - 2, widthFloat-24, 2)];
            [CATransaction commit];
        }
        _selectSegument = segument;
        [self.delegate segumentSelectionChange:_selectSegument];
    }
}

- (void)setSelectSegument:(NSInteger)selectSegument{
    [self.btnTitleSource[_selectSegument] setSelected:NO];
    [self.btnTitleSource[selectSegument] setSelected:YES];
    _selectSegument = selectSegument;
    [self setNeedsLayout];
}

-(void)setBottomColor:(UIColor *)bottomColor{
    _bottomColor = bottomColor;
    [self setNeedsLayout];
}

- (void)setBottomLineColor:(UIColor *)bottomLineColor{
    _bottomLineColor = bottomLineColor;
    [self setNeedsLayout];
}

-(void)setTitleColor:(UIColor *)titleColor{
    _titleColor = titleColor;
    [self setNeedsLayout];
}

-(void)setTitleFont:(UIFont *)titleFont{
    _titleFont = titleFont;
    [self setNeedsLayout];
}

-(void)setSelectColor:(UIColor *)selectColor{
    _selectColor = selectColor;
    [self setNeedsLayout];
}

- (void)selectSegementAtIndex:(NSInteger)index{
    if (_selectSegument != index) {
        [self.btnTitleSource[_selectSegument] setSelected:NO];
        [self.btnTitleSource[index] setSelected:YES];
        
        if (self.isAnimated) {
            [bottomLayer setFrame:CGRectMake(index * widthFloat+12, self.bounds.size.height - 2, widthFloat-24, 2)];
        }else{
            [CATransaction begin];
            [CATransaction setDisableActions:YES];
            [bottomLayer setFrame:CGRectMake(index * widthFloat+12, self.bounds.size.height - 2, widthFloat-24, 2)];
            [CATransaction commit];
        }
        _selectSegument = index;
//        [self.delegate segumentSelectionChange:_selectSegument];
    }

}

@end
